package com.tcm.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.ListUtils;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.redis.JedisClient;
import com.tcm.content.dao.mapper.PanelSlideMapper;
import com.tcm.content.dto.PanelSlideDTO;
import com.tcm.content.entity.PanelSlide;
import com.tcm.content.enums.BusinessCodeEnum;
import com.tcm.content.service.PanelSlideService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", delay = -1, retries = 3)
public class PanelSlideServiceImpl implements PanelSlideService {

    private final static Logger log = LoggerFactory.getLogger(PanelServiceImpl.class);

    @Autowired
    private PanelSlideMapper tbPanelContentMapper;

    @Autowired
    private JedisClient jedisClient;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;


    @Value("${SLIDE_PANEL_ID}")
    private String SLIDE_PANEL_ID;

    @Override
    public String getSlidePanelId() {
        return SLIDE_PANEL_ID;
    }

    @Override
    public int addPanelContent(PanelSlideDTO panelContent) throws ServiceException {
        PanelSlide tbPanelContent = new PanelSlide();
        BeanUtils.copyProperties(panelContent, tbPanelContent);
        tbPanelContent.setId(uidGenerator.getUID());
        tbPanelContent.setCreateTime(DateUtil.date());
        tbPanelContent.setUpdateTime(DateUtil.date());
        tbPanelContent.setStatus(new Byte("1"));
        if (tbPanelContentMapper.insert(tbPanelContent) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_ADD_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public List<PanelSlideDTO> listPanelSlideByPanelId(Long panelId) {
       List<PanelSlide> panelSlides = tbPanelContentMapper.listSlide(panelId);
       if(ListUtils.isEmpty(panelSlides)){
           return null;
       }
       return panelSlides.stream().map(panelSlide -> {
           PanelSlideDTO panelSlideDTO = new PanelSlideDTO();
           BeanUtils.copyProperties(panelSlide, panelSlideDTO);
           return panelSlideDTO;
       }).collect(Collectors.toList());
    }

    @Override
    public int deletePanelSlide(Long id) throws ServiceException {
        if (tbPanelContentMapper.delete(id) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_DELETE_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public int updateContent(PanelSlideDTO panelContent) throws ServiceException {
        PanelSlide tbPanelContent = new PanelSlide();
        BeanUtils.copyProperties(panelContent, tbPanelContent);
        PanelSlideDTO old = findPanelSlideById(panelContent.getId());
        if(ObjectUtil.isEmpty(old)){
            R.throwFail(BusinessCodeEnum.PANEL_UPDATE_FAIL);
        }
        tbPanelContent.setUpdateTime(DateUtil.date());
        if (tbPanelContentMapper.update(tbPanelContent) != 1) {
            R.throwFail(BusinessCodeEnum.PANEL_UPDATE_FAIL);
        }
        //同步缓存
        deleteHomeRedis();
        return 1;
    }

    @Override
    public PanelSlideDTO findPanelSlideById(Long id) {
        PanelSlide tbPanelContent = tbPanelContentMapper.findById(id);
        if (tbPanelContent == null) {
            R.throwFail(BusinessCodeEnum.FAIL_FIND_PANEL_SLIDE_BY_ID);
        }
        PanelSlideDTO panelContent = new PanelSlideDTO();
        BeanUtils.copyProperties(tbPanelContent, panelContent);
        return panelContent;
    }

    @Override
    public String findSlideRedis() {
        try {
            String json = jedisClient.get(SLIDE_PANEL_ID);
            return json;
        } catch (Exception e) {
            log.error(e.toString());
        }
        return "";
    }

    @Override
    public int updateSlideRedis() {
        try {
            jedisClient.del(SLIDE_PANEL_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 同步首页缓存
     */
    public void deleteHomeRedis() {
        try {
            jedisClient.del(SLIDE_PANEL_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
